package com.example.mycamera;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SurFaceCamera extends Activity implements OnClickListener{
    SurfaceView surfaceView;
    Button button;
    android.hardware.Camera camera=null;
    File currentFile=null;
    SurfaceHolder.Callback sCallback=new SurfaceHolder.Callback() {
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			stopPreView();
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			startView();
		}
		

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub
			
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sur_face_camera);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		surfaceView=(SurfaceView) findViewById(R.id.sur_preview);
		button=(Button) findViewById(R.id.btn_photo);
		button.setOnClickListener(this);
		surfaceView.getHolder().addCallback(sCallback);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sur_face_camera, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		camera.takePicture(null, null, new PictureCallback() {
			
			@Override
			public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
				// TODO Auto-generated method stub
				if(saveFile(data)!=null){
					Toast.makeText(getApplicationContext(), "success", 199).show();
				}
			}
		});
	}
	protected File saveFile(byte[] data) {
		// TODO Auto-generated method stub
		File dir=new File(Environment.getExternalStorageDirectory(),"myphoto");
		if(!dir.exists()){
			dir.mkdirs();
		}
		currentFile=new File(dir, System.currentTimeMillis()+".jpg");
		if(!currentFile.exists()){
			try{
				currentFile.createNewFile();
				FileOutputStream fos=new FileOutputStream(currentFile);
				fos.write(data);
				fos.flush();
				fos.close();
				return currentFile;
			}catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return null;
	}

	private void stopPreView() {
		// TODO Auto-generated method stub
		camera.stopPreview();
		camera.release();
		camera=null;
	}
	protected void startView() {
		// TODO Auto-generated method stub
        camera=android.hardware.Camera.open();
        try{
        	camera.setDisplayOrientation(90);
        	camera.setPreviewDisplay(surfaceView.getHolder());
        	camera.startPreview();
        }catch (IOException e) {
			// TODO: handle exception
        	e.printStackTrace();
		}
	}

}
